//
// Created by xd on 2020/4/13.
//


#include "Common.h"
#include "cn_study_aviplayer_BitmapPlayerActivity.h"
extern "C" {
#include "avilib/avilib.h"
}
#include <android/bitmap.h>

extern "C"
JNIEXPORT jboolean JNICALL
Java_cn_study_aviplayer_BitmapPlayerActivity_render(JNIEnv *env, jclass clazz, jlong avi,
                                                    jobject bitmap) {

    jboolean isFrameRead = JNI_FALSE;
    char *frameBuffer = 0;
    long frameSize = 0;
    int keyFrame = 0;

    if (0 > AndroidBitmap_lockPixels(env, bitmap, (void **) &frameBuffer)) {

        ThrowException(env, "java/io/IOException", "不能够锁定像素");
        goto exit;
    }

    //将avi帧byte读取到bitmap中
    frameSize = AVI_read_frame((avi_t *) avi, frameBuffer, &keyFrame);

    //解锁
    if (0 > AndroidBitmap_unlockPixels(env, bitmap)) {

        ThrowException(env, "java/io/IOException", "不能够解锁像素");
        goto exit;
    }
    //是否读取成功
    if (0 < frameSize) {

        isFrameRead = JNI_TRUE;
    }
    exit:
    return isFrameRead;

}
